package com.ai.aicontact.busi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.aicontact.busi.entity.CriticalPointCatagory;
import com.ai.aicontact.busi.entity.CriticalPointDef;
import com.ai.aicontact.busi.service.CriticalPointService;
import com.ai.aicontact.busi.vo.CriticalPointCatagoryVO;
import com.ai.aicontact.common.vo.PageFilter;
import com.ai.aicontact.core.dao.BaseDaoI;

@Service
public class CriticalPointServiceImpl implements CriticalPointService {

	@Autowired
	private BaseDaoI<CriticalPointCatagory> pointCatagoryDao;

	@Autowired
	private BaseDaoI<CriticalPointDef> criticalPointDefDao;

	
	public void saveCriticalPointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		if (criticalPointCatagory.getCatagoryId() != null && criticalPointCatagory.getCatagoryId() > 0) {
			CriticalPointCatagory catagoryFDB = pointCatagoryDao.get(CriticalPointCatagory.class, criticalPointCatagory.getCatagoryId());
			catagoryFDB.setCatagoryName(criticalPointCatagory.getCatagoryName());
			catagoryFDB.setCatagoryLevel(criticalPointCatagory.getCatagoryLevel());
			catagoryFDB.setParentId(criticalPointCatagory.getParentId());
			catagoryFDB.setSortBy(criticalPointCatagory.getSortBy());
			catagoryFDB.setRemarks(criticalPointCatagory.getRemarks());
			pointCatagoryDao.update(catagoryFDB);
		} else {
			pointCatagoryDao.save(criticalPointCatagory);
		}
	}

	public void delCriticalPointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		CriticalPointCatagory criticalPointCatagoryFDB = pointCatagoryDao.get(CriticalPointCatagory.class, criticalPointCatagory.getCatagoryId());
		pointCatagoryDao.delete(criticalPointCatagoryFDB);
	}
	
	
	

	@Override
	public List<CriticalPointCatagoryVO> getCriticalPointCatagory(CriticalPointCatagory criticalPointCatagory) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();

		String sql = "select t.CATAGORY_ID as \"catagoryId\", t.CATAGORY_NAME as \"catagoryName\",t.CATAGORY_LEVEL as \"catagoryLevel\",t.PARENT_ID  as \"parentId\", t.CREATE_DATE as \"createDate\", t.REMARKS  as \"remarks\", t.SORT_BY as \"sortBy\", p.CATAGORY_NAME as \"parentName\" from CRITICAL_POINT_CATAGORY t left join CRITICAL_POINT_CATAGORY p on t.PARENT_ID = p.CATAGORY_ID";
		sql += whereSqlCriticalPointCatagory(criticalPointCatagory, params) +" START WITH t.PARENT_ID = 0  CONNECT BY PRIOR t.CATAGORY_ID = t.PARENT_ID AND t.state=1  ORDER SIBLINGS BY t.SORT_BY,t.CATAGORY_ID  ";
		List<CriticalPointCatagoryVO> list = pointCatagoryDao.findEntityBySql(sql , params, CriticalPointCatagoryVO.class);
 		return list;
	}
	
	private String whereSqlCriticalPointCatagory(CriticalPointCatagory criticalPointCatagory, Map<String, Object> params) {
		String sql = " where t.state=1 ";
		if (criticalPointCatagory != null) { 
			if (StringUtils.isNotBlank(criticalPointCatagory.getCatagoryName())) {
				sql += " and t.CATAGORY_NAME like :catagoryName";
				params.put("catagoryName", "%" +criticalPointCatagory.getCatagoryName() + "%");
			}
			if (StringUtils.isNotBlank(criticalPointCatagory.getRemarks())) {
				sql += " and t.remarks like :remarks";
				params.put("remarks", "%" + criticalPointCatagory.getRemarks() + "%");
			}
			if (criticalPointCatagory.getCatagoryId() != null && criticalPointCatagory.getCatagoryId() > 0) {
				sql += " and t.CATAGORY_ID = :catagoryId";
				params.put("catagoryId",  criticalPointCatagory.getCatagoryId());
			}

			if (criticalPointCatagory.getParentId() != null && criticalPointCatagory.getParentId() > 0) {
				sql += " and t.PARENT_ID = :parentId";
				params.put("parentId",  criticalPointCatagory.getParentId());
			}
		}
		return sql;
	}

 
	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	
	@Override
	public List<CriticalPointDef> getCriticalPointDef(CriticalPointDef criticalPointDef, PageFilter pf) throws Exception {
		String hql = "from CriticalPointDef t ";
		Map<String, Object> params = new HashMap<String, Object>();
		List<CriticalPointDef> list = criticalPointDefDao.find(hql + whereHqlPointDef(criticalPointDef, params)+orderHql(pf) , params);
 		return list;
	}
	
	@Override
	public Long countCriticalPointDef(CriticalPointDef criticalPointDef) throws Exception {
		String hql = "select count(*) from CriticalPointDef t ";
		Map<String, Object> params = new HashMap<String, Object>();
		Long count = criticalPointDefDao.count(hql + whereHqlPointDef(criticalPointDef, params) , params);
 		return count;
	}
	
	public void saveCriticalPointDef(CriticalPointDef criticalPointDef) throws Exception {
		if (criticalPointDef.getCriticalPointId() != null && criticalPointDef.getCriticalPointId() > 0) {
			CriticalPointDef criticalPointDefFDB = criticalPointDefDao.get(CriticalPointDef.class, criticalPointDef.getCriticalPointId());
//			criticalPointDefFDB.setCatagoryId(criticalPointDef.getCatagoryId());
			criticalPointDefFDB.setCriticalPointDec(criticalPointDef.getCriticalPointDec());
			criticalPointDefFDB.setCriticalPointName(criticalPointDef.getCriticalPointName());
//			criticalPointDefFDB.setCriticalPointStatus(criticalPointDef.getCriticalPointStatus());
			criticalPointDefFDB.setCriticalPointType(criticalPointDef.getCriticalPointType());
			criticalPointDefFDB.setEffectiveDate(criticalPointDef.getEffectiveDate());
			criticalPointDefFDB.setExpireDate(criticalPointDef.getEffectiveDate());
			criticalPointDefFDB.setRemarks(criticalPointDef.getRemarks());
			criticalPointDefFDB.setExt1(criticalPointDef.getExt1());
			criticalPointDefFDB.setExt2(criticalPointDef.getExt2());
			criticalPointDefDao.update(criticalPointDefFDB);
		}else{ 
			criticalPointDefDao.save(criticalPointDef);
		}
	}
	
	public void delCriticalPointDef(CriticalPointDef criticalPointDef) throws Exception {
		criticalPointDefDao.delete(criticalPointDef);
	}

	private String whereHqlPointDef(CriticalPointDef criticalPointDef, Map<String, Object> params) {
		String hql = "";
		if (criticalPointDef != null) {
			hql += " where 1=1 ";
			if (StringUtils.isNotBlank(criticalPointDef.getCriticalPointName())) {
				hql += " and t.criticalPointName like :criticalPointName";
				params.put("criticalPointName", "%" +criticalPointDef.getCriticalPointName() + "%");
			}
			if (StringUtils.isNotBlank(criticalPointDef.getRemarks())) {
				hql += " and t.remarks like :remarks";
				params.put("remarks", "%" + criticalPointDef.getRemarks() + "%");
			}
			if (StringUtils.isNotBlank(criticalPointDef.getCriticalPointDec())) {
				hql += " and t.criticalPointDec like :criticalPointDec";
				params.put("criticalPointDec", "%" + criticalPointDef.getCriticalPointDec() + "%");
			}
			if (criticalPointDef.getCatagoryId() != null && criticalPointDef.getCatagoryId() > 0) {
				hql += " and t.catagoryId = :catagoryId";
				params.put("catagoryId",  criticalPointDef.getCatagoryId());
			}

			if (criticalPointDef.getCriticalPointId()!= null && criticalPointDef.getCriticalPointId() > 0) {
				hql += " and t.criticalPointId = :criticalPointId";
				params.put("criticalPointId",  criticalPointDef.getCriticalPointId());
			}

			if (criticalPointDef.getCriticalPointStatus()!= null && criticalPointDef.getCriticalPointStatus() > 0) {
				hql += " and t.criticalPointStatus = :criticalPointStatus";
				params.put("criticalPointStatus",  criticalPointDef.getCriticalPointStatus());
			}

			if (criticalPointDef.getChannelId() != null && criticalPointDef.getChannelId() > 0) {
				hql += " and t.channelId = :channelId";
				params.put("channelId",  criticalPointDef.getChannelId());
			}
		}
		return hql;
	}

	@Override
	public CriticalPointCatagory getCriticalPointCatagoryById(Long catagoryId) throws Exception {
		return pointCatagoryDao.get(CriticalPointCatagory.class, catagoryId);
	}

	
	@Override
	public List<CriticalPointDef> getCriticalPointDefs() throws Exception {
		String hql = "from CriticalPointDef t ";
		List<CriticalPointDef> list = criticalPointDefDao.find(hql);
		return list;
	}

	 
 

	@Override
	public CriticalPointDef getCriticalPointDefById(Long criticalPointId) throws Exception {
		CriticalPointDef criticalPointDef =  criticalPointDefDao.get(CriticalPointDef.class, criticalPointId);
		return criticalPointDef;
	}


}
